﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using aerowan.common.Network;
using System.IO;
using System.Windows;
using aerowan.pilot_client.Network.Packets;

namespace aerowan.pilot_client.Network
{
    public class ServerLayer
    {
        public static ServerLayer Instance { get; private set; }
        static ServerLayer()
        {
            Instance = new ServerLayer();
        }

        NetConnection NetClient;

        ServerLayer()
        {
            PacketTypeManager.RegisterClientPacketType(0x06, typeof(SCPkt_Login));
        }

        public bool Open()
        {
            NetClient = new NetConnection(IPAddress.Parse(Directory.Exists("c:\\awloc") ? "127.0.0.1" : "91.121.171.104"), 8477);
            NetClient.Closed += new NetConnection.NetConnectionClosedEventHandler(() =>
            {
                ApplicationContext.Instance.Connected = false;
            });
            return NetClient.Init();
        }

        public void Close()
        {
            NetClient.OnDisconnect(true);
            NetClient = null;
        }

        public void SendPacket(BaseLocalPacket pkt)
        {
            NetClient.SendPacket(pkt);
        }
    }
}
